package coolapk.cj.cj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import coolapk.cj.cj.entity.Goods;
import coolapk.cj.cj.entity.GroupMember;
import coolapk.cj.cj.mapper.GoodsMapper;
import coolapk.cj.cj.mapper.GroupMemberMapper;
import coolapk.cj.cj.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Objects;

/**
 * @author wenjunjun9
 * @created 2021-02-16 18:51
 * @description
 */
@Slf4j
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GroupMemberMapper groupMemberMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addOrUpdateGoods(Goods goods) {
        // 修改，只允许修改时间和名称
        if (Objects.nonNull(goods.getId())) {
            Goods byId = getById(goods.getId());
            if (Objects.isNull(byId)) {
                log.error("要修改的商品不存在, id = {}", goods.getId());
                throw new IllegalArgumentException("要修改的商品不存在！");
            }
            byId.setName(goods.getName());
            byId.setModifiedTime(LocalDateTime.now());
            byId.setLunar(goods.getLunar());
            updateById(byId);
            return;
        }

        LambdaQueryWrapper<GroupMember> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GroupMember::getQq, goods.getProvider());
        GroupMember groupMember = groupMemberMapper.selectOne(queryWrapper);
        if (groupMember == null) {
            log.error("您不是群成员，无法添加奖品！");
            throw new IllegalArgumentException("您不是群成员，无法添加奖品！");
        }

        goods.setCreateTime(LocalDateTime.now());
        goods.setModifiedTime(LocalDateTime.now());
        goods.setWinner(null);
        goods.setWinnerTime(null);
        save(goods);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteGoods(String id) {
        Goods goods = getById(id);
        if (Objects.isNull(goods)) {
            log.error("要删除的商品不存在, id = {}", id);
            throw new IllegalArgumentException("要修改的商品不存在！");
        }
        removeById(id);
    }

    /**
     * 倒序查询所有奖品
     *
     * @param page
     * @return
     */
    @Override
    public IPage<Goods> pageQuery(Page<Goods> page) {
        Wrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery()
                .orderByDesc(Goods::getPublishTime);
        Page<Goods> pageResult = page(page, wrapper);
        return pageResult;
    }

    /**
     * 倒序查询已经开奖的数据
     *
     * @param page
     * @return
     */
    @Override
    public IPage<Goods> pageWinner(Page<Goods> page) {
        Wrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery()
                .ne(Goods::getWinner, null)
                .orderByDesc(Goods::getWinnerTime);

        Page<Goods> result = page(page, wrapper);
        return result;
    }

    /**
     * 现在功能只支持当天抽奖商品一同开奖
     * @return
     */
    @Override
    public List<Goods> queryCurrentGoods() {
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        Wrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery()
                .apply("date_format(publish_time,'%Y-%m-%d') = {0}", formatter.format(currentDate));
        List<Goods> list = list(wrapper);
        return list;
    }
}
